//
//  Rocket.swift
//  ShapeSurvival
//
//  Created by Jerry H on 1/15/15.
//  Copyright (c) 2015 HeylmunProgramming. All rights reserved.
//

import Foundation; import SpriteKit;

class Rocket: Enemy {
    var animationArray = Array<SKTexture>();
    var animationAtlas = SKTextureAtlas(named: "RocketAtlas.atlas")
    var onTexture = 0;
    var special: CGFloat = 0.00 // fuel, ect.
    var special2: CGFloat = 0.00 // Actual speed= when momentum
    
    override func setStats() {
        //Rocket
        type = 10
        special = 10
        animationAtlas = SKTextureAtlas(named: "RocketAtlas.atlas")
        animationArray.append(animationAtlas.textureNamed("rocket1"))
        animationArray.append(animationAtlas.textureNamed("rocket2"))
        health = 100;
        movespeed = CGFloat(250 + Int(arc4random_uniform(100)))
        special2 = movespeed
        rotatesPerSecond = 1.5
        facing = true;
        chase = true;
        canBeShot = false;
        spawnTime = runTime
        emomentum = 1.5
        self.xScale = 0.25;
        self.yScale = 0.25;
        
    }
    
    override func facePlayer() -> SKAction {
        let angTo = angleToPlayer(true)
        let timelapse = ((runTime - lastRotate)) * rotatesPerSecond
        let t = (timelapse * 40)
        if((angTo < 20 && angTo > -20) || distanceToPlayer() < 150  || ( distanceToPlayer() < 250 && angTo < 90 && angTo > -90)){
            if((special2 + (t * 3)) > (movespeed + 250)){
                special2 = movespeed + 250
            }
            else{
                special2 = special2 + (t * 3)
            }
        }
        else{
            if((special2 - (t * 1.5)) < (movespeed - 50)){
                special2 = movespeed - 50
            }
            else{
                special2 = special2 - ( t * 1.5)
            }
        }
        if((distanceToPlayer() < 250) ) { // || (distanceToPlayer() < 250 && angTo > 90 && angTo < -90)){
            lastRotate = runTime
            return(SKAction.rotateByAngle(0.00, duration: 0.0))
        }
        let invLapse = timelapse * -1
        var turning = (-1 * angleToPlayer(false))
        lastRotate = runTime
        if(turning > timelapse){
            return(SKAction.rotateByAngle(timelapse, duration: 0.0))
        }
        else if(turning < invLapse){
            return(SKAction.rotateByAngle(invLapse, duration: 0.0))
        }
        else {
            return(SKAction.rotateByAngle(turning, duration: 0.0))
        }
    }
    
    override func setSpeedBoost() -> CGFloat {
        return special2 / 50
    }
    
    override func moveSelf(PlayerX: CGFloat, PlayerY: CGFloat ) {
        
        checkChanges()
        if(self.facing == true){
            if(self.chase == true){
                let face = facePlayer()
                let move = moveByFacing(emomentum)
                self.runAction(SKAction.group([move, face]))
            }
            //self.runAction(move)
        }
        else if(self.chase == true){
            self.runAction(moveWithoutFacing())
        }
        else if(runTime > spawnTime + special){
            //do nothing
        }
        else{
            // Go straight
            self.runAction(moveByFacing(emomentum))
        }
        
    }
    
    override func checkChanges(){
        lifeTime = CGFloat(CGFloat(runTime) - CGFloat(spawnTime))
        if(self.chase == true){
            if( runTime > spawnTime + special) {
                
                self.texture = animationAtlas.textureNamed("Rocket")
                self.chase = false
                self.facing = false
                
                self.runAction(self.moveByFacing(special2 / 100), completion: {
                    self.die()
                })
                
                /*let red = UIColor(red: 255, green: 0, blue: 0, alpha: 1.0)
                let recolor = SKAction.colorizeWithColor(red, colorBlendFactor: 50 , duration: CFTimeInterval(special2 / 100))
                let action = self.moveByFacing(special2 / 100)
                self.runAction(SKAction.group([recolor,action]), completion: {
                self.die()
                })*/
                
            }
            else{
                
                if( onTexture != 1){
                    onTexture = 1
                    self.texture = animationAtlas.textureNamed("rocket1")
                }
                else{
                    onTexture = 2
                    self.texture = animationAtlas.textureNamed("rocket2")
                }
            }
        }
        
    }
    
    override func die(){
        self.runAction(SKAction.scaleTo(0.0, duration: 0.1), completion: {
            self.createExplosion(2, elength: 2.0, esize: 2.0, epoint: self.position)
            let tempE = find(eManager.currentEnemies, self)
            var setE = -1
            if(tempE? != nil){
                setE = Int(tempE!)
                if(setE != -1){
                    eManager.currentEnemies.removeAtIndex(setE)
                    self.removeFromParent()
                    score = score + (self.value)
                }
            }
            
        })
    }
}